package com.jade.explorer.ui.exp;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.jade.explorer.dirscan.FileSystem;
import com.jade.explorer.views.JadeSharedImages;
import com.jade.logger.Logger;

public class ExportDomain implements IExportTarget{
	
	private String _title = "Domain";
	private String _localPath = "";
	private Composite _layer = null;
	private HashMap<String, String> _road = new HashMap<String, String>();
	private ArrayList<String> _finalList = new ArrayList<String>();
	private ArrayList<String> _nameList = new ArrayList<String>();
	private String _expPath = "";
	private boolean _result = false;
	private Button _b = null;
	private boolean _over;

	@Override
	public void addTargetItem(Tree t) {
		TreeItem ti = new TreeItem(t, 0);
		ti.setText(_title);
		ti.setImage(JadeSharedImages.getImage(JadeSharedImages.IMG_DOMAIN_GORIZOTAL));
	}
	
	@Override
	public void setLocalPath(String localPath) {
		_localPath = localPath + "\\Placement";
	}

	@Override
	public boolean getResult() {
		return _result;
	}

	@Override
	public void run() {
		if (ready()){
			
			_result = true;
			
			for(String s : _finalList){
				if (_over){
					if (!FileSystem.copyFileToWithOverwrite(_road.get(s), _expPath)) 
						_result = false;
				}
				else{
					if (!FileSystem.copyFileToWithNoOverwrite(_road.get(s), _expPath)) 
						_result = false;
				}
					
			}
			
		}
		else{
			Logger.print(Logger.STATE_ERROR, "Export path or files were not selected!");
		}
		
	}

	@Override
	public boolean ready() {
		
		if (_finalList.size() > 0 && _expPath != "")
			return true;
		return false;
	}

	@Override
	public void setVisible(boolean b) {
		_layer.setVisible(b);
	}

	@Override
	public void setEnabled(boolean b) {
		_layer.setEnabled(b);
	}

	@Override
	public String getTitle() {
		return _title;
	}

	@Override
	public Composite createLayer(Composite parent) {
		
		fillData();
		
		Composite _layer = new Composite(parent, SWT.NONE);
		_layer.setBounds(X, Y, WIDTH, HEIGHT);
		
		if (_localPath != ""){
		
			ListViewer listViewer = new ListViewer(_layer, SWT.BORDER | SWT.V_SCROLL);
			final List primList = listViewer.getList();
			primList.setBounds(10, 78, 170, 239);
			
			for (String s : _nameList){
				primList.add(s);
			}
			
			
			ListViewer listViewer_1 = new ListViewer(_layer, SWT.BORDER | SWT.V_SCROLL);
			final List finalList = listViewer_1.getList();
			finalList.setBounds(264, 78, 170, 239);
			
			Button addButton = new Button(_layer, SWT.NONE);
			addButton.setBounds(183, 129, 78, 25);
			addButton.setText("Add >");
			addButton.addSelectionListener(new SelectionAdapter(){
				
				public void widgetSelected(SelectionEvent e){
					if (primList.getSelectionCount() > 0){
						finalList.add(primList.getItem(primList.getSelectionIndex()));
						_finalList.add(primList.getItem(primList.getSelectionIndex()));
						primList.remove(primList.getSelectionIndex());
						if (_expPath != "")
							_b.setEnabled(true);
					}
				}
			});
			
			Button addAllButton = new Button(_layer, SWT.NONE);
			addAllButton.setBounds(183, 160, 78, 25);
			addAllButton.setText("Add All >");
			addAllButton.addSelectionListener(new SelectionAdapter(){
				
				public void widgetSelected(SelectionEvent e){
					if (primList.getItemCount() > 0){
						finalList.removeAll();
						_finalList.clear();
						primList.removeAll();
						if (_expPath != "")
							_b.setEnabled(true);
						
						for (String s : _nameList){
							finalList.add(s);
							_finalList.add(s);
						}
					}
				}
			});
			
			Button delButton = new Button(_layer, SWT.NONE);
			delButton.setBounds(183, 217, 78, 25);
			delButton.setText("< Delete");
			delButton.addSelectionListener(new SelectionAdapter(){
				
				public void widgetSelected(SelectionEvent e){
					
					if (finalList.getSelectionCount() > 0){
						primList.add(finalList.getItem(finalList.getSelectionIndex()));
						_finalList.remove(finalList.getSelectionIndex());
						finalList.remove(finalList.getSelectionIndex());
					}
					
					if (finalList.getItemCount() == 0)
						_b.setEnabled(false);

				}
			});
			
			Button delAllButton = new Button(_layer, SWT.NONE);
			delAllButton.setBounds(183, 248, 78, 25);
			delAllButton.setText("< Delete All");
			delAllButton.addSelectionListener(new SelectionAdapter(){
				
				public void widgetSelected(SelectionEvent e){
					
					if (finalList.getItemCount() > 0){
						primList.removeAll();
						finalList.removeAll();
						_finalList.clear();
						_b.setEnabled(false);
						
						for (String s : _nameList){
							primList.add(s);
						}
					}

				}
			});
			
			final Text pathText = new Text(_layer, SWT.BORDER);
			pathText.setEditable(false);
			pathText.setBounds(10, 10, 251, 21);
			
			Button pathButton = new Button(_layer, SWT.NONE);
			pathButton.setBounds(267, 8, 167, 25);
			pathButton.setText("Choose path");
			final Shell shell = parent.getShell();
			pathButton.addSelectionListener(new SelectionAdapter(){
				
				public void widgetSelected(SelectionEvent e){
					DirectoryDialog dialog = new DirectoryDialog(shell);
					String exportPath = dialog.open(); 
					
					if (exportPath != null){
						pathText.setText(exportPath);
						_expPath = exportPath;
						if (_finalList.size() > 0)
							_b.setEnabled(true);
					}
				}
			});
			
			Label toExportLabel = new Label(_layer, SWT.NONE);
			toExportLabel.setBounds(10, 57, 170, 15);
			toExportLabel.setText("Files in project:");
			
			Label exportedLabel = new Label(_layer, SWT.NONE);
			exportedLabel.setBounds(264, 57, 170, 15);
			exportedLabel.setText("Files to export:");
			
			Button overWriteCheckButton = new Button(_layer, SWT.CHECK);
			overWriteCheckButton.setBounds(10, 35, 170, 16);
			overWriteCheckButton.setText("Overwrite existing files");
			overWriteCheckButton.setSelection(false);
			overWriteCheckButton.addSelectionListener(new SelectionAdapter(){
				
				public void widgetSelected(SelectionEvent e){
					
					if (_over) _over = false;
					else _over = true;
					
				}
			});
			
		
		}
		
		return _layer;
	}
	
	
	private void fillData(){
		
		if (_localPath != ""){
			File dir = new File(_localPath);
			
			FileFilter ff = new FileFilter(){
				@Override
				public boolean accept(File f) {
					return f.getName().toLowerCase().endsWith(".domain");
				}
			};
			
			for (File f : dir.listFiles(ff)){
				_road.put(f.getName(), f.getPath());
				_nameList.add(f.getName());
			}
		}
		
	}

	@Override
	public void buttonControl(Button b) {
		_b = b;
	}
	
	@Override
	public void clearData(){
		_road.clear();
		_nameList.clear();
		_finalList.clear();
		_expPath = "";
		_localPath = "";
	}

}
